package com.quick.controller.system;

import com.quick.common.annotation.Log;
import com.quick.common.core.controller.BaseController;
import com.quick.common.core.domain.ApiResponse;
import com.quick.common.enums.BusinessTypeEnum;
import com.quick.common.utils.poi.ExcelUtil;
import com.quick.system.domain.SysConfig;
import com.quick.system.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 参数配置管理
 *
 * @Author: Administrator
 * @Date: 2023/7/17
 */
@Api(value = "参数管理", tags = {"参数配置操作接口"})
@RestController
@RequestMapping("/system/config")
public class SysConfigController extends BaseController {
    @Autowired
    private SysConfigService sysConfigService;


    @GetMapping("/list")
    @ApiOperation(value = "获取所有参数配置列表")
    @PreAuthorize("@ps.hasPermission('system:config:list')")
    public ApiResponse list(SysConfig sysConfig) {
        try {
            startPage();
            return paging(sysConfigService.selectConfigList(sysConfig));
        } finally {
            clearPage();
        }
    }

    @PostMapping("/export")
    @ApiOperation(value = "导出所有参数配置列表")
    @Log(title = "参数管理", businessType = BusinessTypeEnum.EXPORT)
    @PreAuthorize("@ps.hasPermission('system:config:export')")
    public void export(HttpServletResponse response, SysConfig sysConfig) {
        ExcelUtil<SysConfig> util = new ExcelUtil<>(SysConfig.class);
        List<SysConfig> sysConfigs = sysConfigService.selectConfigList(sysConfig);
        util.exportExcel(response, sysConfigs, "参数配置");
    }

    /**
     * 根据参数编号获取详细信息
     */
    @GetMapping(value = "/{configId}")
    @ApiOperation(value = "根据configId查询参数配置")
    @PreAuthorize("@ps.hasPermission('system:config:query')")
    public ApiResponse getInfo(@PathVariable Long configId) {
        return success(sysConfigService.selectConfigById(configId));
    }

    /**
     * 新增参数配置
     */
    @PostMapping
    @ApiOperation(value = "新增参数配置")
    @Log(title = "参数管理", businessType = BusinessTypeEnum.INSERT)
    @PreAuthorize("@ps.hasPermission('system:config:add')")
    public ApiResponse add(@Validated @RequestBody SysConfig sysConfig) {
        return success(sysConfigService.insertConfig(sysConfig));
    }

    /**
     * 修改参数配置
     */
    @PutMapping
    @ApiOperation(value = "修改参数配置")
    @Log(title = "参数管理", businessType = BusinessTypeEnum.UPDATE)
    @PreAuthorize("@ps.hasPermission('system:config:edit')")
    public ApiResponse edit(@Validated @RequestBody SysConfig sysConfig) {
        return success(sysConfigService.updateConfig(sysConfig));
    }

    /**
     * 删除参数配置
     */
    @DeleteMapping("/{configIds}")
    @ApiOperation(value = "删除参数配置")
    @Log(title = "参数管理", businessType = BusinessTypeEnum.DELETE)
    @PreAuthorize("@ps.hasPermission('system:config:delete')")
    public ApiResponse remove(@PathVariable Long[] configIds) {
        sysConfigService.deleteConfigByIds(configIds);
        return success();
    }

    /**
     * 刷新参数缓存
     */
    @PreAuthorize("@ps.hasPermission('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessTypeEnum.CLEAN)
    @DeleteMapping("/refreshCache")
    public ApiResponse refreshCache() {
        sysConfigService.resetConfigCache();
        return success();
    }
}
